package com.tbsoft.ehr.service.resume;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;

import com.tbsoft.ehr.dao.index.IndexBuildTaskDao;
import com.tbsoft.ehr.dao.resume.ResumeDao;
import com.tbsoft.ehr.model.IndexBuildTask;
import com.tbsoft.ehr.model.ProjectExperience;
import com.tbsoft.ehr.model.Resume;

/**
 * @desc 简历索引service实现
 * @author vidy
 * 
 */
public class ResumeIndexServiceImpl implements ResumeIndexService {
	private Log log =LogFactory.getLog(getClass());

	@Override
	public void buildIndex() {
		log.info("buildIndex start....");
		try{
		List<IndexBuildTask> buildList = indexBuildTaskDao
				.getNoRunBuildTaskList("resume");
		for (IndexBuildTask task : buildList) {
			if ("A".equals(task.getOperate()) || "U".equals(task.getOperate())) {
				Resume resume = resumeDao.getResumeById(task.getBusinessId());
				Document doc = new Document();
				doc.add(new Field("ID",resume.getId(),Store.YES,Index.NOT_ANALYZED));
				doc.add(new Field("name",resume.getName(),Store.YES,Index.NOT_ANALYZED));
				doc.add(new Field("post",resume.getPost(),Store.YES,Index.NOT_ANALYZED));
				doc.add(new Field("educationLevel",resume.getEducationLevel(),Store.YES,Index.NOT_ANALYZED));
				doc.add(new Field("occupationalCharacteristics",resume.getOccupationalCharacteristics(),
						Store.NO,Index.ANALYZED));
				doc.add(new Field("technicalExpertise",resume.getTechnicalExpertise(),
						Store.NO,Index.ANALYZED));
				doc.add(new Field("selfEvaluation",resume.getSelfEvaluation(),
						Store.NO,Index.ANALYZED));
				for(int i =0,len = resume.getProjectExperienceList().size();i<len;i++){
					ProjectExperience pe = resume.getProjectExperienceList().get(i);
					doc.add(new Field("projectSummary",pe.getProjectSummary(),
							Store.NO,Index.ANALYZED));
					doc.add(new Field("jobDuties",pe.getJobDuties(),
							Store.NO,Index.ANALYZED));
					doc.add(new Field("mainTechnical",pe.getMainTechnical(),
							Store.NO,Index.ANALYZED));
				}
				if ("A".equals(task.getOperate()) ){
					indexWriter.addDocument(doc);
				}else{
					indexWriter.updateDocument(new Term("ID",resume.getId()), doc);
				}
			}else if ("D".equals(task.getOperate())){
				indexWriter.deleteDocuments(new Term("ID",task.getBusinessId()));
			}
			indexWriter.commit();
			task.setTaskStatus("1");
			indexBuildTaskDao.saveOrUpdate(task);

		}
		}catch(Exception e){
			log.error("buildIndex exception:", e);
		}
		log.info("buildIndex end....");
	}

	private ResumeDao resumeDao;

	private IndexBuildTaskDao indexBuildTaskDao;

	public void setResumeDao(ResumeDao resumeDao) {
		this.resumeDao = resumeDao;
	}

	public void setIndexBuildTaskDao(IndexBuildTaskDao indexBuildTaskDao) {
		this.indexBuildTaskDao = indexBuildTaskDao;
	}
	
	private IndexWriter indexWriter;

	public void setIndexWriter(IndexWriter indexWriter) {
		this.indexWriter = indexWriter;
	}
	
	

}
